package com.winderinfo.yikaotianxia.core.mvp;


import com.google.gson.Gson;
import com.winderinfo.yikaotianxia.core.http.NetworkRequest;

import java.util.Map;

public class Model implements Contract.Model {

    @Override
    public void get(String url, Map<String, Object> map, final Class kind, final Contract.CallBack callback) {
        NetworkRequest.getNetworkRequest().get(url, map, new NetworkRequest.HttpListener() {
            @Override
            public void success(String data) {
                //gson解析
                Gson gson = new Gson();
                Object json = gson.fromJson(data, kind);
                callback.onSuccess(json);
            }

            @Override
            public void failure(String data) {
                callback.onFailure(data);
            }
        });
    }

    @Override
    public void post(String url, Map<String, Object> map, final Class kind, final Contract.CallBack callBack) {
        NetworkRequest.getNetworkRequest().post(url, map, new NetworkRequest.HttpListener() {
            @Override
            public void success(String data) {
                //gson解析
                Gson gson = new Gson();
                Object json = gson.fromJson(data, kind);
                callBack.onSuccess(json);
            }

            @Override
            public void failure(String data) {
                callBack.onFailure(data);
            }
        });
    }

    @Override
    public void delete(String url, Map<String, Object> map, final Class kind, final Contract.CallBack callBack) {
        NetworkRequest.getNetworkRequest().delete(url, map, new NetworkRequest.HttpListener() {
            @Override
            public void success(String data) {
                //gson解析
                Gson gson = new Gson();
                Object json = gson.fromJson(data, kind);
                callBack.onSuccess(json);
            }

            @Override
            public void failure(String data) {
                callBack.onFailure(data);
            }
        });
    }

    @Override
    public void put(String url, Map<String, Object> map, final Class kind, final Contract.CallBack callBack) {
        NetworkRequest.getNetworkRequest().put(url, map, new NetworkRequest.HttpListener() {
            @Override
            public void success(String data) {
                //gson解析
                Gson gson = new Gson();
                Object json = gson.fromJson(data, kind);
                callBack.onSuccess(json);
            }

            @Override
            public void failure(String data) {
                callBack.onFailure(data);
            }
        });
    }

    @Override
    public void imagePast(String url, Map<String, String> map, final Class kind, final Contract.CallBack callBack) {

        NetworkRequest.getNetworkRequest().imagePost(url, map, new NetworkRequest.HttpListener() {
            @Override
            public void success(String data) {
                //gson解析
                Gson gson = new Gson();
                Object json = gson.fromJson(data, kind);
                callBack.onSuccess(json);
            }

            @Override
            public void failure(String data) {
                callBack.onFailure(data);
            }
        });
    }
}

